package com.rpframework.website.device.act.api;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.misc.Service;

import com.google.gson.JsonObject;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.TOrderScore;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.OrderScoreService;

@Controller
@RequestMapping("api/score")
public class ApiOrderScoreAct extends AdminAct{
	@Resource OrderScoreService service;
	
	@RequestMapping(value="/add",produces = "application/json; charset=utf-8")
	public @ResponseBody String add(
			@RequestParam(value = "orderId")  String orderId,
			@RequestParam(value = "speed") Integer speed,
			@RequestParam(value = "trait") Integer trait,
			@RequestParam(value = "server") Integer server,
			@RequestParam(value = "content",required = false ) String content,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User user = getSessionUser(session);
		if(user == null){
			throw new APICodeException(-4, "你还没登陆!");
		}	
		TOrderScore t = new TOrderScore();
		t.setOrderId(orderId);
		t.setSpeed(speed);
		t.setTrait(trait);
		t.setServer(server);
		t.setUserId(user.getId());
		if(StringUtils.isNotBlank(content)){
			t.setContent(content);
		}
		t.setCreateTime(System.currentTimeMillis()/1000);
		boolean flag = service.insert(t);
		JsonObject json=new JsonObject();
		if(flag){
			json.addProperty("code", 200);
			json.addProperty("msg", "评价成功！");
		}else{
			json.addProperty("code", 500);
			json.addProperty("msg", "评价失败！");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
}
